Java demo coding - Wrapping up an applet in a full screen window
Bit pathetic that little applet displayed in your browser isn't it? Proper demos are full screen. Here's an applet that puts another applet in the centre of a full screen window and closes the window when ESC is pressed.
Getting a full screen display
The first thing to do is create a borderless window. In fact to create a Window we need to have a parent Frame. We also want it so the demo 'takes over' from the browser window. To do this we scan through the parents of our applet until a Frame is found that can be latched onto. If we find a NULL as a parent without having found a Frame we'll create a Frame of our own and use this to parent our Window. This new Frame is shown and sized as small as possible. Take a look at the code below which is put in the init() method of the applet:
tempContainer=this;
do{
try{
tempContainer=tempContainer.getParent();
}
catch (NullPointerException e){
// couldn't find a frame to create window from
// so make one
tempContainer=new Frame();
tempContainer.setBounds(0,0,0,0);
tempContainer.setVisible(true);
break;
}
} while(!(tempContainer instanceof Frame));
myFrame=(Frame)tempContainer;
Right, we'll create the Window using
Window myWindow=new Window(myFrame);
then get the size of the screen
ScreenWidth=myWindow.getToolkit().getScreenSize().width;
ScreenHeight=myWindow.getToolkit().getScreenSize().height;
Finally we set the window to fill the screen and display it
myWindow.setLocation(0,0);
myWindow.setSize(ScreenWidth,ScreenHeight);
myWindow.setVisible(true);
You'll probably want to set the background colour of the window as well so use
myWindow.setBackground(Color.black);
with your choice of colour instead of Color.black.
Getting rid of the window
Another fairly trivial (but important) thing that needs adding is the catching of keypresses to close the window. Add the functions
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ESCAPE) stop();
}
public void keyReleased(KeyEvent e){
}
to the applet to handle the keypresses. The keyPressed() function just stops the demo if the escape key is pressed. Just change VK_ESCAPE to another key to exit on other key presses. Also add
myWindow.addKeyListener(this);
in the init() method of the applet.
The stop() method just needs to have
myWindow.setVisible(false); in it.
Putting another applet in the window
Now we've got a blank window we can add another applet to the window. In init() construct the Applet and set its size
myApplet=new DemoApplet(this);
myApplet.setSize(640,480);
I'm now going to skip what a layout manager is and all the different types etc., except to say they control where components (applets, butons, and loads of other stuff) are placed and that we're going to use BorderLayout. So, set the layout manager and then add the newly created Applet by putting
myWindow.setLayout(new BorderLayout());
myWindow.add(myApplet);
in the init() method of our Applet. Then call myApplet.init();
to initialize the applet.
Now the start method needs to have a few things in it, namely making sure the window is shown and in the foreground, putting the Applet in the centre of the window and starting our new Applet.
myWindow.setVisible(true);
myWindow.toFront();
myWindow.requestFocus();
myApplet.setBounds(ScreenWidth/2-640/2,ScreenHeight/2-480/2,640,480);
myApplet.start();
The stop() method needs to call
myApplet.stop();
as well as hiding the window.
Finally make sure destroy() calls
myApplet.destroy();
That's about it
I think I've covered all the main points there. Have a look at the source (AppletLoader.java) for an applet that does all of the above if your confused about how to slot all this together. A few points though:
- The applet that is shown in the window needs to use its parent for getCodeBase(), getDocumentBase(), and other methods if you want to get useful values.
- I'm not sure how keypresses are passed on in Java so it's probably best if your demo is non-interactive (ie. the applet shown in the window does not implement KeyListener).
- I've had problems with the applet centring. Sometimes it does and sometimes it doesn't. I haven't a clue why but it might just be my computer playing up :) I would really appreciate feedback on this point as I've spent hours trying to figure it out.
Feel free to email me if you've any questions, comments, etc.